package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

import java.io.IOException;

/**
 * SubChoiceRespData类
 * 实现响应数据选择的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class SubChoiceRespData implements AxdrType {

    private byte[] dataCode = null;
    private SubChoiceRespDataChoice choice = SubChoiceRespDataChoice._ERR_NONE_SELECTED;
    private int length = 0;
    private AxdrEnum dar = null;
    private SubSeqOfRecords records = null;

    /**
     * 默认构造函数
     */
    public SubChoiceRespData() {
    }

    /**
     * 设置长度
     * 
     * @param length 长度
     */
    public void setLength(int length) {
        this.length = length;
    }

    /**
     * 设置数据编码
     * 
     * @param dataCode 数据编码
     */
    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    /**
     * 设置DAR
     * 
     * @param dar 数据访问结果
     */
    public void setDar(AxdrEnum dar) {
        this.resetChoices();
        this.choice = SubChoiceRespDataChoice.DAR;
        this.dar = dar;
    }

    /**
     * 设置记录
     * 
     * @param records 记录序列
     */
    public void setRecords(SubSeqOfRecords records) {
        this.resetChoices();
        this.choice = SubChoiceRespDataChoice.RECORDS;
        this.records = records;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = SubChoiceRespDataChoice.fromValue((int) choosen.getValue());

        if (this.choice == SubChoiceRespDataChoice.DAR) {
            this.dar = new AxdrEnum();
            this.dar.setConst();
            codeLength += this.dar.decode(input);
            return codeLength;
        } else if (this.choice == SubChoiceRespDataChoice.RECORDS) {
            this.records = new SubSeqOfRecords();
            this.records.setDataLength(this.length);
            codeLength += this.records.decode(input);
            return codeLength;
        }
        throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            try {
                for (int i = this.dataCode.length - 1; i >= 0; i--) {
                    output.write(this.dataCode[i]);
                }
            } catch (IOException e) {
                throw new RuntimeException("Error writing dataCode to output stream", e);
            }
            return this.dataCode.length;
        }
        if (this.choice == SubChoiceRespDataChoice._ERR_NONE_SELECTED) {
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        if (this.choice == SubChoiceRespDataChoice.RECORDS) {
            if (this.records != null) {
                codeLength += this.records.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("records is null");
            }
        } else if (this.choice == SubChoiceRespDataChoice.DAR) {
            codeLength += this.dar.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(0);
            codeLength += c.encode(output);
            return codeLength;
        }

        throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding SubChoiceRespData", e);
        }
        this.dataCode = revOStream.getArray();
    }

    /**
     * 获取选择索引
     * 
     * @return 选择索引
     */
    public SubChoiceRespDataChoice getChoiceIndex() {
        return this.choice;
    }

    /**
     * 获取DAR
     * 
     * @return 数据访问结果
     */
    public AxdrEnum getDar() {
        return this.dar;
    }

    /**
     * 获取记录
     * 
     * @return 记录序列
     */
    public SubSeqOfRecords getRecords() {
        return this.records;
    }

    /**
     * 重置选择
     */
    public void resetChoices() {
        this.choice = SubChoiceRespDataChoice._ERR_NONE_SELECTED;
        this.dar = null;
        this.records = null;
    }

    @Override
    public String toString() {
        if (this.choice == SubChoiceRespDataChoice.DAR) {
            if (this.dar != null) {
                return "choice: {dar: " + this.dar + "}";
            } else {
                return "choice is dar but dar is null";
            }
        } else if (this.choice == SubChoiceRespDataChoice.RECORDS) {
            if (this.records != null) {
                return "choice: {records: " + this.records + "}";
            } else {
                return "choice is records but records is null";
            }
        } else {
            return "unknown";
        }
    }
}